<?php
namespace Tbf\Component\Arrays;
/**
 * Grid数组表示方法: []map[string]string
 * - {a:1,b:2}
 * - {a:3,b:4}
 * TitleArray对应表示法: [][]string
 * - [a,b]
 * - [1,2]
 * - [3,4]
 * 特殊情况:
 * Grid: [] TitleArray: []
 */
class GridFunction {
    public static function fromTitleArray(array $title_array){
        if (empty($title_array)){
            return array();
        }
        $output = array();
        $title_row = $title_array[0];
        unset($title_array[0]);
        foreach($title_array as $row){
            $this_row = array();
            foreach($row as $k1=>$v1){
                $this_row[$title_row[$k1]] = $v1;
            }
            $output[] = $this_row;
        }
        return $output;
    }
    public static function toTitleArray(array $grid){
        if (empty($grid)){
            return array();
        }
        $output = array();
        $output[] = array_keys($grid[0]);
        foreach($grid as $row){
            $output[] = array_values($row);
        }
        return $output;
    }
}